<{assign var="page_title" value="管理用户组" }>
<{include file="FASHION/page_header.html"}>

<h3><{$page_title}></h3>

<ul id="sortable">
<{foreach item=g from=$list}>
<li class="ui-state-default" group_id="<{$g.group_id}>">
	<span id="title_<{$g.group_id}>"><{$g.title}></span>
	<span class="state-menu">
		<a href="javascript:;" onclick="modify('<{$g.group_id}>')" class="icon_s modify" title="修改"></a>
		<a href="javascript:;" onclick="del('<{$g.group_id}>')" class="icon_s remove" title="删除"></a>
	</span>
</li>
<{/foreach}>
</ul>

<input type="button" value="新建" onclick="add();" />
<input type="button" value="Save Ordered" onclick="save_ordered()" />

<div id="dialog" title="新建用户组" style="display:none;">
	<form action="<{url a='group-save'}>" method="post" id="form1">
		组名：<input type="text" name="title" id="title" class="text ui-widget-content ui-corner-all" value="" />
		<input type="hidden" name="group_id" id="group_id" value="" />
	</form>
</div>

<script>
$("#dialog").dialog({
	bgiframe:	1,
	resizable:	1,
	autoOpen:	0,
	height:		130,
	width:		220,
	modal:		1,
	buttons: {
		'取消': function() {
			$(this).dialog('close');
			$("#form1").get(0).reset();
		},
		'保存': function() {
			$("#form1").submit();
		}
	}
});

$("#sortable").sortable();
$("#sortable").disableSelection();

$("#form1").ajaxForm({
	'beforeSubmit' : function(){
		if(!$("#title").val().trim()) {
			alert("请填写正确的组名");
			return false;
		}
	},
	'success' :	function(jsn){
		if(jsn.level=='notice')	//成功
		{
			if(jsn.html)
			{
				if(jsn.method=='insert') {
					$("#sortable").prepend(jsn.html);
				}
				else {
					$("#sortable li[group_id="+jsn.group_id+"]").replaceWith(jsn.html);
				}
			}
			$("#form1").get(0).reset();
			$("#dialog").dialog('close');
		}
		alert(jsn.message);
	}
});

function add()
{
	$('#dialog').dialog('open');
	$("#form1").get(0).reset();
	$("#group_id").val('');
}

function modify(group_id)
{
	$("#group_id").val(group_id);
	$("#title").val($("#title_"+group_id).html());
	$('#dialog').dialog('open');
}

function del(group_id)
{
	if(confirm('确认删除?'))
	{
		$.get('<{$root}>/group-delete/group_id/'+group_id, function(jsn){
			if(jsn.level=='notice') {
				$("#sortable li[group_id="+group_id+"]").remove();
			}
			alert(jsn.message);
		});
	}
}

function save_ordered()
{
	var ary	=	new Array();
	$("#sortable li").each(function(){
		ary.push($(this).attr('group_id'));
	})
	$.get('<{$root}>/group-sort/s/'+ary.toString(),function(jsn){
		alert(jsn.message);
	});
}
</script>

<{include file="FASHION/page_footer.html"}>